import AttackArrow from './AttackArrow';
import {Point} from '../../type';
import {PlotTypes} from '../../enum/PlotTypes';
import * as PlotUtils from '../../utils';
import * as Constants from '../../constants';

export default class SquadCombat extends AttackArrow {

    // public tailWidthFactor: number = 0.1;

    constructor(coordinates: any, points: Array<Point>, params: any) {
        super(coordinates, points, params);
        this._type = PlotTypes.SQUAD_COMBAT;
    }

    protected generate(): void {
        try {
            let count: number = this.getPointCount();
            if (count < 2) {
                return;
            } else {
                let points: Array<Point> = this.getPoints();
                let tailPoints: Array<Point> = this.getTailPoints(points);
                let headPoints: Array<Point> = this.getArrowHeadPoints(points, tailPoints[0], tailPoints[1]) as Array<Point>;
                let neckLeft: Point = headPoints[0];
                let neckRight: Point = headPoints[4];
                let bodyPoints: Array<Point> = this.getArrowBodyPoints(points, neckLeft, neckRight, this.tailWidthFactor);
                let count: number = bodyPoints.length;
                let leftPoints: Array<Point> = [tailPoints[0]].concat(bodyPoints.slice(0, count / 2));
                leftPoints.push(neckLeft);
                let rightPoints: Array<Point> = [tailPoints[1]].concat(bodyPoints.slice(count / 2, count));
                rightPoints.push(neckRight);
                leftPoints = PlotUtils.getQBSplinePoints(leftPoints);
                rightPoints = PlotUtils.getQBSplinePoints(rightPoints);
                // @ts-ignore
                this.setCoordinates([leftPoints.concat(headPoints, rightPoints.reverse())]);
            }
        } catch (e) {
            console.log(e);
        }
    }

    private getTailPoints(points: Array<Point>): Array<Point> {
        let allLen: number = PlotUtils.getBaseLength(points);
        let tailWidth: number = allLen * this.tailWidthFactor;
        let tailLeft: Point = PlotUtils.getThirdPoint(points[1], points[0], Constants.HALF_PI, tailWidth, false);
        let tailRight: Point = PlotUtils.getThirdPoint(points[1], points[0], Constants.HALF_PI, tailWidth, true);
        return [tailLeft, tailRight];
    }
}